//
//  StarterController.m
//  YunboPro
//
//  Created by Kuangming Shi on 9/1/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "StarterController.h"
#import "AllLoginController.h"
#import "SettingController.h"
#import "SendMessageController.h"
#import "ManagerAdapter.h"
#import "QVerifyWebViewController.h"
#import "Messagelist.h"
#import "BaseManager.h"
static Messagelist * list;
static Messagelist *list4;
static Messagelist *list3;


@interface StarterController (private)
    -(void)sendMessage:(id)sender;
    -(void)setSetting:(id)sender;

    -(void)displayAllLoginView;
    -(void)hideAllLoginView;
@end

@implementation StarterController

@synthesize dataMgr = _dataMgr;
@synthesize tableViewController = _tableViewController;
@synthesize loginViewController = _loginViewController;

@synthesize type = _type;


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)dealloc
{
    [_dataMgr release];
    [_tableViewController release];    
    [_loginViewController release];
    [super dealloc];
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];   
    // Do any additional setup after loading the view from its nib.
    NSLog(@"viewDidLoad:%i",self.type);
	ManagerAdapter *managerAdapter = [ManagerAdapter instance];

     
    if (self.type == 1){
        self.view.backgroundColor = [UIColor redColor];
        self.navigationItem.title = @"All--YunboPro";   
    }else if (self.type == 2){
        self.view.backgroundColor = [UIColor grayColor];
        self.navigationItem.title = @"Sina--YunboPro";
       
        list = [[Messagelist alloc] initWithNibName:@"Messagelist" bundle:nil];
        [self.view addSubview:list.view];
        
        
	    if ([[managerAdapter getManager:self.type] isLogin]) {
            NSLog(@"----------isLogin()---YES");
                    
        }else
        {
            NSLog(@"----------isLogin()---NO");
            QVerifyWebViewController *verifyController = 
            [[QVerifyWebViewController alloc] initWithHomeViewController:self];
            [self.view addSubview:verifyController.view];
//#warning:where to release verifyController???
        }
    }else if (self.type == 3){
       
        self.view.backgroundColor = [UIColor blackColor];
        self.navigationItem.title = @"Tencent--YunboPro";
        list3 = [[Messagelist alloc] initWithNibName:@"Messagelist" bundle:nil];
        [self.view addSubview:list3.view];
        
		if ([[managerAdapter getManager:self.type] isLogin]) {
            NSLog(@"----------isLogin()---YES");
           
        }else
        {
            NSLog(@"----------isLogin()---NO");
            QVerifyWebViewController *verifyController = 
            [[QVerifyWebViewController alloc] initWithHomeViewController:self];
            [self.view addSubview:verifyController.view];
//#warning:where to release verifyController???
        }
	}else if (self.type == 4){
        self.view.backgroundColor = [UIColor blueColor];
        self.navigationItem.title = @"Renren--YunboPro";
       list4 = [[Messagelist alloc] initWithNibName:@"Messagelist" bundle:nil];
        [self.view addSubview:list4.view];
        
        if ([[managerAdapter getManager:self.type] isSessionValid] ) {
            NSLog(@"-----------isSessionValid---------");
        }else
        {
            NSLog(@"----------isLogin()---NO");
            QVerifyWebViewController *verifyController = 
            [[QVerifyWebViewController alloc] initWithHomeViewController:self];
            [self.view addSubview:verifyController.view];
//#warning:where to release verifyController???
        }     
    }

    // Create "send" button for the nav bar
    UIBarButtonItem *sendButton = [[UIBarButtonItem alloc] 
      initWithBarButtonSystemItem:UIBarButtonSystemItemCompose
      target:self 
      action:@selector(sendMessage:)];

    [self.navigationItem setRightBarButtonItem:sendButton];
    [sendButton release];

    // Create "setting" button for the nav bar
    UIBarButtonItem *settingButton = [[UIBarButtonItem alloc] 
      initWithBarButtonSystemItem:UIBarButtonSystemItemOrganize 
      target:self 
      action:@selector(setSetting:)];

    [self.navigationItem setLeftBarButtonItem:settingButton];
    [settingButton release];

    [self updateUI];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (void)prepareData
{
    BaseManager *manager   = [[ManagerAdapter instance]getManager:self.type];
    
    NSLog(@"prepareData:%i",self.type);
    if (self.type == 2) {
      
        if ([manager isLogin]){
          
            NSArray * data =[manager getHomeMsgWithPageFlage:PAGEFLAG_FIRST nReqNum:10];
            list.statuses = data;
          
        }
    }
    if (self.type == 3) {
        
        if ([manager isLogin]){
            
            NSArray * data =[manager getHomeMsgWithPageFlage:PAGEFLAG_FIRST nReqNum:10];
            list3.statuses = data;
            
        }
    }
    if(self.type == 4)
    {
        id manager = [[ManagerAdapter instance] getManager:self.type];
        
        if ([manager isSessionValid]){
            NSArray * data = [manager getStatus];
            list4.statuses = data;
            
        }
        
    }
}


- (void)updateUI
{
    if ([self isViewLoaded] == false) return;
    NSLog(@"updateUI:%i",self.type);
    if (self.type == 1) {
        [self displayAllLoginView];
    }
     if (self.type == 2) {
        // [[managerAdapter getManager:self.type] loadDefaultKey];
         BaseManager *manager = [[ManagerAdapter instance] getManager:self.type];
         
         if ([manager isLogin]){
    
         [list.tableViewC reloadData];
         }
         
     }
    if (self.type == 3) {
        // [[managerAdapter getManager:self.type] loadDefaultKey];
        BaseManager *manager = [[ManagerAdapter instance] getManager:self.type];
        
        if ([manager isLogin]){
            
            [list3.tableViewC reloadData];
        }
        
    }
    if (self.type ==4) {
        id *manager = [[ManagerAdapter instance] getManager:self.type];
        
        if ([manager isSessionValid]){
            
            [list4.tableViewC reloadData];
        }
    }
    
    
}

- (void)sendMessage:(id)sender
{
    NSLog(@"sendMessage:%i",self.type);
    SendMessageController * vc = [[SendMessageController alloc] initWithNibName:@"SendMessage" bundle:nil];
    [self.navigationController presentModalViewController:vc animated:NO];
    [vc release];
}

- (void)setSetting:(id)sender
{
    NSLog(@"setSetting:%i",self.type);
    SettingController * vc = [[SettingController alloc] initWithNibName:@"Setting" bundle:nil];
    [self.navigationController presentModalViewController:vc animated:NO];
    [vc release];
}

- (void)displayAllLoginView
{
    if (self.loginViewController) return;

    AllLoginController * allLoginController = [[AllLoginController alloc] 
                                   initWithNibName:@"AllLogin" bundle:nil];

    //[self.navigationController pushViewController:allLoginController animated:NO];
    //allLoginController.navigationItem.hidesBackButton = YES;
    //allLoginController.navigationItem.title = @"Accounts";
    
    [self.view addSubview:allLoginController.view];
    self.loginViewController = allLoginController;
    [allLoginController release];
}

- (void)hideAllLoginView
{
    if (self.loginViewController) {
        [self.loginViewController.view removeFromSuperview];
        [self.loginViewController release];
        self.loginViewController = nil;
    }
}


@end
